package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.mybatis.paginator.domain.Order;
import com.framework.loippi.service.SystemMessageService;
import com.framework.loippi.support.APIPrincipal;
import com.framework.loippi.support.Pageable;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(value = "系统消息模块", description = "系统消息模块", tags = {"系统消息模块"})
@RequestMapping(value = "/api/message")
public class SystemMessageAPIController extends ApiBaseController {

    @Resource
    private SystemMessageService systemMessageService;

    @ApiOperation(value = "消息列表", notes = "",  produces = "application/json", responseContainer = "List", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authentication", value = "authentication,鉴权码", dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "pageNumber", value = "页码", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面显示数量", defaultValue = "10", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败")
    })
    @RequestMapping(value = "/message_list.json", method = RequestMethod.POST)
    @ResponseBody
    public String messageList(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        APIPrincipal principal = getPrincipal(request);
        Pageable pageable = new Pageable(pageNumber, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", principal.getId());
        pageable.setParameter(map);
        pageable.setOrderDirection(Order.Direction.DESC);
        pageable.setOrderProperty("tum.id");
//        Page<UserMessage> page = userMessageService.findMessageByPage(pageable);
//        List<UserMessageResult> build = UserMessageResult.build(page.getContent());
        return ApiUtils.success();
    }


    @ApiOperation(value = "是否有消息", notes = "object里面反 1-有消息,0没有消息", produces = "application/json", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authentication", value = "authentication,鉴权码", dataType = "String", paramType = "header"),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败")
    })
    @RequestMapping(value = "/is_hava_message.json", method = RequestMethod.POST)
    @ResponseBody
    public String isHaveMessage(HttpServletRequest request) {
        APIPrincipal principal = getPrincipal(request);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", principal.getId());
        map.put("status", 2);
//        Long count = userMessageService.count(map);
//        if (count > 0) {
//            return ApiUtils.success(1);
//        }
        return ApiUtils.success(0);
    }

    //登陆信息获取
    private APIPrincipal getPrincipal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        APIPrincipal principal = (APIPrincipal) session.getAttribute(APIPrincipal.PREFIX_USER_SESSION);
        return principal;
    }
}
